//
//  ViewController.m
//  YYPopMenuView
//
//  Created by 蓓蕾 on 2021/8/21.
//

#import "ViewController.h"
#import "FTPopOverMenu.h"

#import "DropOneViewController.h"
#import "DropTwoViewController.h"
#import "LinkageViewController.h"

#define kScreenWidth [[UIScreen mainScreen] bounds].size.width
#define kScreenHeight [[UIScreen mainScreen] bounds].size.height

@interface ViewController ()

@property (nonatomic, strong) NSArray *titleArr;
@property (nonatomic, strong) NSArray *imageArr;


@property (nonatomic, strong) NSArray *longTitleArr;
@property (nonatomic, strong) NSArray *longImageArr;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    NSString *icomImageURLString = @"https://avatars1.githubusercontent.com/u/4414522?v=3&s=40";
    NSURL *icomImageURL = [NSURL URLWithString:icomImageURLString];
    _titleArr = @[@"下拉列表(样式一)", @"下拉列表(样式二)", @"双表联动", @"MenuFour"];
    _imageArr = @[icomImageURLString, icomImageURL, [UIImage imageNamed:@"Pokemon_Go_03"], @"Pokemon_Go_04"];
    
    _longTitleArr = @[@"MenuOne",@"MenuTwo",@"MenuThree",@"MenuFour",@"MenuOne",@"MenuTwo",@"MenuThree",@"MenuFour",@"MenuOne",@"MenuTwo",@"MenuThree",@"MenuFour",@"MenuOne",@"MenuTwo",@"MenuThree",@"MenuFour",@"MenuOne",@"MenuTwo",@"MenuThree",@"MenuFour",@"MenuOne",@"MenuTwo",@"MenuThree",@"MenuFour",@"MenuOne",@"MenuTwo",@"MenuThree",@"MenuFour",@"MenuOne",@"MenuTwo",@"MenuThree",@"MenuFour",@"MenuOne",@"MenuTwo",@"MenuThree",@"MenuFour",@"MenuOne",@"MenuTwo",@"MenuThree",@"MenuFour",@"MenuOne",@"MenuTwo",@"MenuThree",@"MenuFour",@"MenuOne",@"MenuTwo",@"MenuThree",@"MenuFour",@"MenuOne",@"MenuTwo",@"MenuThree",@"MenuFour"];
    _longImageArr = @[@"Pokemon_Go_01",@"Pokemon_Go_02",@"Pokemon_Go_03",@"Pokemon_Go_04",@"Pokemon_Go_01",@"Pokemon_Go_02",@"Pokemon_Go_03",@"Pokemon_Go_04",@"Pokemon_Go_01",@"Pokemon_Go_02",@"Pokemon_Go_03",@"Pokemon_Go_04",@"Pokemon_Go_01",@"Pokemon_Go_02",@"Pokemon_Go_03",@"Pokemon_Go_04",@"Pokemon_Go_01",@"Pokemon_Go_02",@"Pokemon_Go_03",@"Pokemon_Go_04",@"Pokemon_Go_01",@"Pokemon_Go_02",@"Pokemon_Go_03",@"Pokemon_Go_04",@"Pokemon_Go_01",@"Pokemon_Go_02",@"Pokemon_Go_03",@"Pokemon_Go_04",@"Pokemon_Go_01",@"Pokemon_Go_02",@"Pokemon_Go_03",@"Pokemon_Go_04",@"Pokemon_Go_01",@"Pokemon_Go_02",@"Pokemon_Go_03",@"Pokemon_Go_04",@"Pokemon_Go_01",@"Pokemon_Go_02",@"Pokemon_Go_03",@"Pokemon_Go_04",@"Pokemon_Go_01",@"Pokemon_Go_02",@"Pokemon_Go_03",@"Pokemon_Go_04",@"Pokemon_Go_01",@"Pokemon_Go_02",@"Pokemon_Go_03",@"Pokemon_Go_04",@"Pokemon_Go_01",@"Pokemon_Go_02",@"Pokemon_Go_03",@"Pokemon_Go_04"];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addMethod)];
    
    [self initUI];
}

-(void)initUI
{
    UIButton *senderView = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:senderView];
    [senderView setTitle:@"指向View" forState:UIControlStateNormal];
    [senderView setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    senderView.titleLabel.font = [UIFont systemFontOfSize:15];
    [senderView addTarget:self action:@selector(ActionMethod:) forControlEvents:UIControlEventTouchUpInside];
    senderView.frame = CGRectMake(10, 100, 100, 50);
    senderView.backgroundColor = [UIColor orangeColor];
    senderView.layer.cornerRadius = 10;
    senderView.tag = 1;
    
    UIButton *longSenderView = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:longSenderView];
    [longSenderView setTitle:@"超长数据" forState:UIControlStateNormal];
    [longSenderView setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    longSenderView.titleLabel.font = [UIFont systemFontOfSize:15];
    [longSenderView addTarget:self action:@selector(ActionMethod:) forControlEvents:UIControlEventTouchUpInside];
    longSenderView.frame = CGRectMake(CGRectGetMaxX(senderView.frame) + 20, 100, CGRectGetWidth(senderView.bounds), CGRectGetHeight(senderView.bounds));
    longSenderView.backgroundColor = [UIColor orangeColor];
    longSenderView.layer.cornerRadius = 10;
    longSenderView.tag = 2;
    
    UIButton *staticLengthView = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:staticLengthView];
    [staticLengthView setTitle:@"固定长度" forState:UIControlStateNormal];
    [staticLengthView setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    staticLengthView.titleLabel.font = [UIFont systemFontOfSize:15];
    [staticLengthView addTarget:self action:@selector(ActionMethod:) forControlEvents:UIControlEventTouchUpInside];
    staticLengthView.frame = CGRectMake(CGRectGetMaxX(longSenderView.frame) + 20, 100, CGRectGetWidth(senderView.bounds), CGRectGetHeight(senderView.bounds));
    staticLengthView.backgroundColor = [UIColor orangeColor];
    staticLengthView.layer.cornerRadius = 10;
    staticLengthView.tag = 3;
    
    UIButton *staticItemView = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:staticItemView];
    [staticItemView setTitle:@"固定Item" forState:UIControlStateNormal];
    [staticItemView setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    staticItemView.titleLabel.font = [UIFont systemFontOfSize:15];
    [staticItemView addTarget:self action:@selector(ActionMethod:) forControlEvents:UIControlEventTouchUpInside];
    staticItemView.frame = CGRectMake(CGRectGetMinX(senderView.frame), CGRectGetMaxY(senderView.frame) + 20, CGRectGetWidth(senderView.bounds), CGRectGetHeight(senderView.bounds));
    staticItemView.backgroundColor = [UIColor orangeColor];
    staticItemView.layer.cornerRadius = 10;
    staticItemView.tag = 4;
    
    UIButton *noArrowView = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:noArrowView];
    [noArrowView setTitle:@"没有箭头" forState:UIControlStateNormal];
    [noArrowView setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    noArrowView.titleLabel.font = [UIFont systemFontOfSize:15];
    [noArrowView addTarget:self action:@selector(noArrowActionMethod:) forControlEvents:UIControlEventTouchUpInside];
    noArrowView.frame = CGRectMake(CGRectGetMinX(senderView.frame), kScreenHeight - CGRectGetHeight(senderView.bounds) - 20, CGRectGetWidth(senderView.bounds), CGRectGetHeight(senderView.bounds));
    noArrowView.backgroundColor = [UIColor orangeColor];
    noArrowView.layer.cornerRadius = 10;
    noArrowView.tag = 1;
    
    UIButton *noArrowLongView = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:noArrowLongView];
    [noArrowLongView setTitle:@"超长数据" forState:UIControlStateNormal];
    [noArrowLongView setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    noArrowLongView.titleLabel.font = [UIFont systemFontOfSize:15];
    [noArrowLongView addTarget:self action:@selector(noArrowActionMethod:) forControlEvents:UIControlEventTouchUpInside];
    noArrowLongView.frame = CGRectMake(CGRectGetMaxX(noArrowView.frame) + 20, CGRectGetMinY(noArrowView.frame), CGRectGetWidth(senderView.bounds), CGRectGetHeight(senderView.bounds));
    noArrowLongView.backgroundColor = [UIColor orangeColor];
    noArrowLongView.layer.cornerRadius = 10;
    noArrowLongView.tag = 2;
    
    UIButton *noArrowStaticLengthView = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:noArrowStaticLengthView];
    [noArrowStaticLengthView setTitle:@"固定长度" forState:UIControlStateNormal];
    [noArrowStaticLengthView setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    noArrowStaticLengthView.titleLabel.font = [UIFont systemFontOfSize:15];
    [noArrowStaticLengthView addTarget:self action:@selector(noArrowActionMethod:) forControlEvents:UIControlEventTouchUpInside];
    noArrowStaticLengthView.frame = CGRectMake(CGRectGetMaxX(noArrowLongView.frame) + 20, CGRectGetMinY(noArrowView.frame), CGRectGetWidth(senderView.bounds), CGRectGetHeight(senderView.bounds));
    noArrowStaticLengthView.backgroundColor = [UIColor orangeColor];
    noArrowStaticLengthView.layer.cornerRadius = 10;
    noArrowStaticLengthView.tag = 3;
}

//-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
//{
//    
//    FTPopOverMenuConfiguration *configuration = [FTPopOverMenuConfiguration defaultConfiguration];
//    configuration.hiddenArrow = YES;
////    configuration.menuMaxHeight = 40 * 6 + 15;
////    configuration.backgroundColor = [UIColor orangeColor];
////    [FTPopOverMenu showForSender:sender withMenuArray:_longTitleArr imageArray:_longImageArr configuration:configuration doneBlock:^(NSInteger selectedIndex) {
////        NSLog(@"selectedIndex = %ld",selectedIndex);
////    } dismissBlock:^{
//////            NSLog(@"取消本次操作");
////    }];
//
//    [FTPopOverMenu showForPoint:CGPointMake(100, 400) withMenuArray:_longTitleArr imageArray:_longImageArr configuration:configuration doneBlock:^(NSInteger selectedIndex) {
//        NSLog(@"selectedIndex = %ld",selectedIndex);
//    } dismissBlock:^{
//        NSLog(@"取消本次操作");
//    }];
//
////    [FTPopOverMenu showFromEvent:event withMenuArray:_longTitleArr imageArray:_longImageArr configuration:configuration doneBlock:^(NSInteger selectedIndex) {
////        NSLog(@"selectedIndex = %ld",selectedIndex);
////    } dismissBlock:^{
////        NSLog(@"取消本次操作");
////    }];
//}

-(void)noArrowActionMethod:(UIButton *)sender
{
    NSInteger tag = sender.tag;
    if (tag == 1) {
        FTPopOverMenuConfiguration *configuration = [FTPopOverMenuConfiguration defaultConfiguration];
        configuration.hiddenArrow = YES;
        configuration.menuTopMargin = 5;
        [FTPopOverMenu showForSender:sender withMenuArray:_titleArr imageArray:_imageArr configuration:configuration doneBlock:^(NSInteger selectedIndex) {
            NSLog(@"selectedIndex = %ld",selectedIndex);
        } dismissBlock:^{
            NSLog(@"取消本次操作");
        }];
    }
    else if (tag == 2)
    {
//        NSLog(@"kScreenHeight = %lf",kScreenHeight);
        FTPopOverMenuConfiguration *configuration = [FTPopOverMenuConfiguration defaultConfiguration];
        configuration.hiddenArrow = YES;
        [FTPopOverMenu showForSender:sender withMenuArray:_longTitleArr imageArray:_longImageArr configuration:configuration doneBlock:^(NSInteger selectedIndex) {
            NSLog(@"selectedIndex = %ld",selectedIndex);
        } dismissBlock:^{
//            NSLog(@"取消本次操作");
        }];
    }
    else if (tag == 3)
    {
//        NSLog(@"kScreenHeight = %lf",kScreenHeight);
        FTPopOverMenuConfiguration *configuration = [FTPopOverMenuConfiguration defaultConfiguration];
        configuration.hiddenArrow = YES;
        configuration.menuMaxHeight = 40 * 6 + 15;
        [FTPopOverMenu showForSender:sender withMenuArray:_longTitleArr imageArray:_longImageArr configuration:configuration doneBlock:^(NSInteger selectedIndex) {
            NSLog(@"selectedIndex = %ld",selectedIndex);
        } dismissBlock:^{
//            NSLog(@"取消本次操作");
        }];
    }
    else if (tag == 4)
    {
        FTPopOverMenuConfiguration *configuration = [FTPopOverMenuConfiguration defaultConfiguration];
        configuration.maxMenuItemCount = 6;
        [FTPopOverMenu showForSender:sender withMenuArray:_longTitleArr imageArray:_longImageArr configuration:configuration doneBlock:^(NSInteger selectedIndex) {
            NSLog(@"selectedIndex = %ld",selectedIndex);
        } dismissBlock:^{
            NSLog(@"取消本次操作");
        }];
    }
    else if (tag == 5)
    {
        
    }
    else if (tag == 6)
    {
        FTPopOverMenuConfiguration *configuration = [FTPopOverMenuConfiguration defaultConfiguration];
        configuration.hiddenArrow = YES;
        configuration.menuTopMargin = 5;
        [FTPopOverMenu showForSender:sender withMenuArray:_titleArr imageArray:_imageArr configuration:configuration doneBlock:^(NSInteger selectedIndex) {
            NSLog(@"selectedIndex = %ld",selectedIndex);
        } dismissBlock:^{
            NSLog(@"取消本次操作");
        }];
    }
}

-(void)ActionMethod:(UIButton *)sender
{
    NSInteger tag = sender.tag;
    if (tag == 1) {
        [FTPopOverMenu showForSender:sender withMenuArray:_titleArr imageArray:_imageArr doneBlock:^(NSInteger selectedIndex) {
            NSLog(@"selectedIndex = %ld",selectedIndex);
        } dismissBlock:^{
            NSLog(@"取消本次操作");
        }];
    }
    else if (tag == 2)
    {
        [FTPopOverMenu showForSender:sender withMenuArray:_longTitleArr imageArray:_longImageArr doneBlock:^(NSInteger selectedIndex) {
            NSLog(@"selectedIndex = %ld",selectedIndex);
        } dismissBlock:^{
            NSLog(@"取消本次操作");
        }];
    }
    else if (tag == 3)
    {
        FTPopOverMenuConfiguration *configuration = [FTPopOverMenuConfiguration defaultConfiguration];
        configuration.menuMaxHeight = 40 * 6 + 15;
        [FTPopOverMenu showForSender:sender withMenuArray:_longTitleArr imageArray:_longImageArr configuration:configuration doneBlock:^(NSInteger selectedIndex) {
            NSLog(@"selectedIndex = %ld",selectedIndex);
        } dismissBlock:^{
            NSLog(@"取消本次操作");
        }];
    }
    else if (tag == 4)
    {
        FTPopOverMenuConfiguration *configuration = [FTPopOverMenuConfiguration defaultConfiguration];
        configuration.maxMenuItemCount = 6;
        [FTPopOverMenu showForSender:sender withMenuArray:_longTitleArr imageArray:_longImageArr configuration:configuration doneBlock:^(NSInteger selectedIndex) {
            NSLog(@"selectedIndex = %ld",selectedIndex);
        } dismissBlock:^{
            NSLog(@"取消本次操作");
        }];
    }
    else if (tag == 5)
    {
        FTPopOverMenuConfiguration *configuration = [FTPopOverMenuConfiguration defaultConfiguration];
        configuration.hiddenArrow = YES;
        configuration.menuTopMargin = 5;
        [FTPopOverMenu showForSender:sender withMenuArray:_titleArr imageArray:_imageArr configuration:configuration doneBlock:^(NSInteger selectedIndex) {
            NSLog(@"selectedIndex = %ld",selectedIndex);
        } dismissBlock:^{
            NSLog(@"取消本次操作");
        }];
    }
    else if (tag == 6)
    {
        FTPopOverMenuConfiguration *configuration = [FTPopOverMenuConfiguration defaultConfiguration];
        configuration.hiddenArrow = YES;
        configuration.menuTopMargin = 5;
        [FTPopOverMenu showForSender:sender withMenuArray:_titleArr imageArray:_imageArr configuration:configuration doneBlock:^(NSInteger selectedIndex) {
            NSLog(@"selectedIndex = %ld",selectedIndex);
        } dismissBlock:^{
            NSLog(@"取消本次操作");
        }];
    }
}

-(void)addMethod
{
    YYMenuWeakSelf
    FTPopOverMenuConfiguration *configuration = [FTPopOverMenuConfiguration defaultConfiguration];
    configuration.menuWidth = 160;
    [FTPopOverMenu showForPoint:CGPointMake(kScreenWidth - 35, 64) withMenuArray:_titleArr imageArray:_imageArr configuration:configuration doneBlock:^(NSInteger selectedIndex) {
        NSLog(@"selectedIndex = %ld",selectedIndex);
        if (selectedIndex == 0) {
            DropOneViewController *dropVC = [[DropOneViewController alloc] init];
            [weakSelf.navigationController pushViewController:dropVC animated:YES];
        }
        else if (selectedIndex == 1)
        {
            DropTwoViewController *dropVC = [[DropTwoViewController alloc] init];
            [weakSelf.navigationController pushViewController:dropVC animated:YES];
        }
        else if (selectedIndex == 2)
        {
            LinkageViewController *linkageVC = [[LinkageViewController alloc] init];
            linkageVC.navigationItem.title = @"双表联动";
            [weakSelf.navigationController pushViewController:linkageVC animated:YES];
        }
    } dismissBlock:^{
        NSLog(@"取消本次操作");
    }];
}


@end
